home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / gcc / ixemul-4.lha / ixemul-41.4 / network / res_mkquery.c < prev    next >
C/C++ Source or Header  |  1995-05-18  |  6KB  |  208 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)res_mkquery.c    6.16 (Berkeley) 3/6/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/param.h>
  39. #include <netinet/in.h>
  40. #include <arpa/nameser.h>
  41. #include <resolv.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. /*
  46.  * Form all types of queries.
  47.  * Returns the size of the result or -1.
  48.  */
  49. res_mkquery(op, dname, class, type, data, datalen, newrr, buf, buflen)
  50.     int op;            /* opcode of query */
  51.     const char *dname;        /* domain name */
  52.     int class, type;    /* class and type of query */
  53.     const char *data;        /* resource record data */
  54.     int datalen;        /* length of data */
  55.     const struct rrec *newrr;    /* new rr for modify or append */
  56.     char *buf;        /* buffer to put query */
  57.     int buflen;        /* size of buffer */
  58. {
  59.     register HEADER *hp;
  60.     register char *cp;
  61.     register int n;
  62.     char *dnptrs[10], **dpp, **lastdnptr;
  63.  
  64. #ifdef DEBUG
  65.     if (_res.options & RES_DEBUG)
  66.         printf("res_mkquery(%d, %s, %d, %d)\n", op, dname, class, type);
  67. #endif DEBUG
  68.     /*
  69.      * Initialize header fields.
  70.      */
  71.     if ((buf == NULL) || (buflen < sizeof(HEADER)))
  72.         return(-1);
  73.     bzero(buf, sizeof(HEADER));
  74.     hp = (HEADER *) buf;
  75.     hp->id = htons(++_res.id);
  76.     hp->opcode = op;
  77.     hp->pr = (_res.options & RES_PRIMARY) != 0;
  78.     hp->rd = (_res.options & RES_RECURSE) != 0;
  79.     hp->rcode = NOERROR;
  80.     cp = buf + sizeof(HEADER);
  81.     buflen -= sizeof(HEADER);
  82.     dpp = dnptrs;
  83.     *dpp++ = buf;
  84.     *dpp++ = NULL;
  85.     lastdnptr = dnptrs + sizeof(dnptrs)/sizeof(dnptrs[0]);
  86.     /*
  87.      * perform opcode specific processing
  88.      */
  89.     switch (op) {
  90.     case QUERY:
  91.         if ((buflen -= QFIXEDSZ) < 0)
  92.             return(-1);
  93.         if ((n = dn_comp((u_char *)dname, (u_char *)cp, buflen,
  94.             (u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
  95.             return (-1);
  96.         cp += n;
  97.         buflen -= n;
  98.         __putshort(type, (u_char *)cp);
  99.         cp += sizeof(u_short);
  100.         __putshort(class, (u_char *)cp);
  101.         cp += sizeof(u_short);
  102.         hp->qdcount = htons(1);
  103.         if (op == QUERY || data == NULL)
  104.             break;
  105.         /*
  106.          * Make an additional record for completion domain.
  107.          */
  108.         buflen -= RRFIXEDSZ;
  109.         if ((n = dn_comp((u_char *)data, (u_char *)cp, buflen,
  110.             (u_char **)dnptrs, (u_char **)lastdnptr)) < 0)
  111.             return (-1);
  112.         cp += n;
  113.         buflen -= n;
  114.         __putshort(T_NULL, (u_char *)cp);
  115.         cp += sizeof(u_short);
  116.         __putshort(class, (u_char *)cp);
  117.         cp += sizeof(u_short);
  118.         __putlong(0, (u_char *)cp);
  119.         cp += sizeof(u_long);
  120.         __putshort(0, (u_char *)cp);
  121.         cp += sizeof(u_short);
  122.         hp->arcount = htons(1);
  123.         break;
  124.  
  125.     case IQUERY:
  126.         /*
  127.          * Initialize answer section
  128.          */
  129.         if (buflen < 1 + RRFIXEDSZ + datalen)
  130.             return (-1);
  131.         *cp++ = '\0';    /* no domain name */
  132.         __putshort(type, (u_char *)cp);
  133.         cp += sizeof(u_short);
  134.         __putshort(class, (u_char *)cp);
  135.         cp += sizeof(u_short);
  136.         __putlong(0, (u_char *)cp);
  137.         cp += sizeof(u_long);
  138.         __putshort(datalen, (u_char *)cp);
  139.         cp += sizeof(u_short);
  140.         if (datalen) {
  141.             bcopy(data, cp, datalen);
  142.             cp += datalen;
  143.         }
  144.         hp->ancount = htons(1);
  145.         break;
  146.  
  147. #ifdef ALLOW_UPDATES
  148.     /*
  149.      * For UPDATEM/UPDATEMA, do UPDATED/UPDATEDA followed by UPDATEA
  150.      * (Record to be modified is followed by its replacement in msg.)
  151.      */
  152.     case UPDATEM:
  153.     case UPDATEMA:
  154.  
  155.     case UPDATED:
  156.         /*
  157.          * The res code for UPDATED and UPDATEDA is the same; user
  158.          * calls them differently: specifies data for UPDATED; server
  159.          * ignores data if specified for UPDATEDA.
  160.          */
  161.     case UPDATEDA:
  162.         buflen -= RRFIXEDSZ + datalen;
  163.         if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
  164.             return (-1);
  165.         cp += n;
  166.         __putshort(type, cp);
  167.                 cp += sizeof(u_short);
  168.                 __putshort(class, cp);
  169.                 cp += sizeof(u_short);
  170.         __putlong(0, cp);
  171.         cp += sizeof(u_long);
  172.         __putshort(datalen, cp);
  173.                 cp += sizeof(u_short);
  174.         if (datalen) {
  175.             bcopy(data, cp, datalen);
  176.             cp += datalen;
  177.         }
  178.         if ( (op == UPDATED) || (op == UPDATEDA) ) {
  179.             hp->ancount = htons(0);
  180.             break;
  181.         }
  182.         /* Else UPDATEM/UPDATEMA, so drop into code for UPDATEA */
  183.  
  184.     case UPDATEA:    /* Add new resource record */
  185.         buflen -= RRFIXEDSZ + datalen;
  186.         if ((n = dn_comp(dname, cp, buflen, dnptrs, lastdnptr)) < 0)
  187.             return (-1);
  188.         cp += n;
  189.         __putshort(newrr->r_type, cp);
  190.                 cp += sizeof(u_short);
  191.                 __putshort(newrr->r_class, cp);
  192.                 cp += sizeof(u_short);
  193.         __putlong(0, cp);
  194.         cp += sizeof(u_long);
  195.         __putshort(newrr->r_size, cp);
  196.                 cp += sizeof(u_short);
  197.         if (newrr->r_size) {
  198.             bcopy(newrr->r_data, cp, newrr->r_size);
  199.             cp += newrr->r_size;
  200.         }
  201.         hp->ancount = htons(0);
  202.         break;
  203.  
  204. #endif ALLOW_UPDATES
  205.     }
  206.     return (cp - buf);
  207. }
  208.